#!/usr/bin/python
# -*- coding: utf-8 -*-

#为方便以后移植，参数中的 key 一律为字符串格式

import datetime

import logging
from google.appengine.ext import db
from google.appengine.api import memcache 
from google.appengine.api import users

import googledatabase
import getcounter
import commonfunction
import databasefunction


#NewlyDays最近天数
def GetFriendsNewBlog(NewlyDays, FriendRecords):
#    SqlCondition = ""
#    for FriendItem in FriendRecords:
#        logging.error( "--------------- GetFriendsNewBlog FriendItem.FriendName = %s" % (FriendItem.FriendName) )
#        if 1 > len(SqlCondition):
#            SqlCondition = " LoginName = '%s'" % FriendItem.FriendName
#        else:
#            SqlCondition += " OR LoginName = '%s'" % FriendItem.FriendName

#    SqlQuery = "SELECT * FROM CMicroblogTable WHERE (%s) AND Date > :1 ORDER BY Date DESC" % (SqlCondition)
    if FriendRecords == None:
        return None

    FriendLoginName = []
    for FriendItem in FriendRecords:
        FriendLoginName += [FriendItem.FriendName]

    CheckTime = datetime.datetime.now() - datetime.timedelta(days = NewlyDays)
    SqlQuery = "SELECT * FROM CMicroblogTable WHERE LoginName IN :1 AND Date > :2 ORDER BY Date DESC"
    Query = db.GqlQuery( SqlQuery, FriendLoginName, CheckTime )

#    Query = googledatabase.CMicroblogTable.all()
#    Query.filter('LoginName IN', FriendLoginName)
#    Query.filter('Date >', CheckTime)
#    Query.order("-Date")

    return Query.fetch(1000)


def GetFriendsNewCollect(NewlyDays, FriendRecords):
    if FriendRecords == None:
        return None

    FriendLoginName = []
    for FriendItem in FriendRecords:
        FriendLoginName += [FriendItem.FriendName]

    CheckTime = datetime.datetime.now() - datetime.timedelta(days = NewlyDays)
    SqlQuery = "SELECT * FROM CMyCollectTable WHERE LoginName IN :1 AND Date > :2 ORDER BY Date DESC"
    Query = db.GqlQuery( SqlQuery, FriendLoginName, CheckTime )

    return Query.fetch(1000)



def GetFriendsNewLike(NewlyDays, FriendRecords):
    if FriendRecords == None:
        return None

    FriendLoginName = []
    for FriendItem in FriendRecords:
        FriendLoginName += [FriendItem.FriendName]

    CheckTime = datetime.datetime.now() - datetime.timedelta(days = NewlyDays)
    SqlQuery = "SELECT * FROM CMyLikeTable WHERE LoginName IN :1 AND Date > :2 ORDER BY Date DESC"
    Query = db.GqlQuery( SqlQuery, FriendLoginName, CheckTime )

    return Query.fetch(1000)

    
